#!/bin/csh -f
if ( $#argv == 0 ) then
   echo "use GFSAVN05D_ingest hours(06/12/18/00) hoursago"
   exit 1
#   set hh = `date -u +%H`
#   if ( ( "$hh" >= 07 ) & ( "$hh" <= 12 ) ) set h2 = 06
#   if ( ( "$hh" >= 13 ) & ( "$hh" <= 18 ) ) set h2 = 12 
#   if ( ( "$hh" >= 19 ) | ( "$hh" <= 00 ) ) set h2 = 18
#   if ( ( "$hh" >= 01 ) & ( "$hh" <= 06 ) ) set h2 = 00
else
   set h2 = $1
   set hoursago = $2
endif

set yyyymmdd = `date -u +%Y%m%d --date "$hoursago hours ago"`
set yymmdd = `date -u +%y%m%d --date "$hoursago hours ago"`
set yyyy = `date -u +%Y  --date "$hoursago hours ago"`
set yyyymm = `date -u +%Y%m  --date "$hoursago hours ago"`

#set savedir = /mnt/node15data3/data/gfs_avn_0.5deg_grb2
#set savedir = /mnt/storagedata2/data/NCEP_gfs_avn_0.5deg
set savedir = /home/dataop/data/test


if (-d $savedir ) then
  cd $savedir
else
  echo "Not find data disk"
  mkdir -p $savedir
  cd $savedir
endif

#set ftpsite = http://nomad1.ncep.noaa.gov
#set ftppath = pub/gfs_master
#set dir = "gfs"$yyyymmdd


set ftpsite = http://nomads.ncep.noaa.gov
set ftppath = pub/data/nccf/com/gfs/prod
set dir = "gfs."$yyyymmdd$1

set finish = 0

set name = "gfs.t"$h2"z.pgrb2f72"
set newname = "gfs05d_"$yymmdd"_"$h2"_72"

set head = "gfs.t"$h2"z.pgrb2f"
set newhead = "gfs05d_"$yymmdd"_"$h2"_"

set LOCK = /tmp/GFSAVN05D_ingest.00-72.$newname

if ( -f $LOCK ) then
   echo "Process is locked, cannot execute"
   set locktime = `stat --format %Y $LOCK`
   set diff = `date +%s --date "10 hours ago"`
   echo $locktime[1] $diff[1]
   if ( $diff[1] > $locktime[1] ) then
      rm -f $LOCK
      echo "$LOCK is created 1 day(s) ago, delete it and start a new ingest process"
   else
      exit 0
   endif
endif
touch $LOCK

set fl = 1
set dn = 0

touch .downloaded.00-72
grep $newname .downloaded.00-72

if ( ! $status ) then
  set dn = 1
else
  set files = $ftpsite"/"$ftppath"/"$dir"/"$name
  echo $files

        set size = 0
        while ( $size < 40000000 )
	   wget $files -O $newname
           if ( $status ) then
             set fl = 0
             break
           else
	     chmod 644 $newname
             set temp  = `wc -c $newname`
             set size =  $temp[1]
           endif
        end

        if( $fl ) then
            echo $newname >> .downloaded.00-72
        endif
endif
rm -f $LOCK

if( $dn | $fl ) then
        foreach f ( 00 03 06 09 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 )
                set fl = 1
      		set files = $ftpsite"/"$ftppath"/"$dir"/"$head$f

		set LOCK = /tmp/GFSAVN05D_ingest.00-72.$newhead$f
		if ( -f $LOCK ) then
		   echo "$newhead$f Process is locked, cannot execute"
   		   set locktime = `stat --format %Y $LOCK`
		   set diff = `date +%s --date "10 hours ago"`
		   echo $locktime[1] $diff[1]
		   if ( $diff[1] > $locktime[1] ) then
		      rm -f $LOCK
			      echo "$LOCK is created 1 day(s) ago, delete it and start a new ingest process"
		   else
		      break;
		   endif
		endif
		touch $LOCK

                grep $newhead$f  .downloaded.00-72
                if ( $status ) then
        		set size = 0
        		while ( $size < 38000000 )
	      			wget $files -O $newhead$f 
                		if( $status ) then
                		    set fl = 0
                		    break
                		else
      	      	    		    chmod 644 $newhead$f
		    		    set temp  = `wc -c $newhead$f`
         	        	    set size =  $temp[1]
                        	endif
			end
			if( $fl ) then
            			echo $newhead$f >> .downloaded.00-72
        		endif		
		endif

		rm -f $LOCK
	end
endif

exit 0


